package com.muqi.app.qlearn.fragments;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.animation.LinearOutSlowInInterpolator;

import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.flyco.banner.anim.select.RotateEnter;
import com.flyco.banner.anim.unselect.NoAnimExist;
import com.flyco.widget.BannerItem;
import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.NetWorkUtils;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.ResponseUtils;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.pj.shops.ShopBannerBean;
import com.muqi.app.project.contants.MContants;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.ScreenUtils;
import com.muqi.app.project.utils.ShowToast;
import com.muqi.app.qlearn.modles.HomeBanner;
import com.muqi.app.qlearn.student.BaseChildFragment;
import com.muqi.app.qlearn.student.R;
import com.muqi.app.qlearn.student.ui.ClassRoomCommentStatisticsActivity;
import com.muqi.app.qlearn.student.ui.ClassroomVedioActivity;
import com.muqi.app.qlearn.student.ui.GrowthReportActivity;
import com.muqi.app.qlearn.student.ui.MainListActivity;
import com.muqi.app.qlearn.student.ui.MoveActivity;
import com.muqi.app.qlearn.student.ui.RewardActivity;
import com.muqi.app.qlearn.ui.MyHomeWorkActivity;
import com.muqi.app.qlearn.ui.SystemNoticeActivity;
import com.muqi.app.user.db.ChildBean;
import com.muqi.app.user.widget.SimpleImageBanner;

public class TabHome extends BaseChildFragment implements OnClickListener {
	private TextView homeTitle;
	private ChildReceiver receiver;
	private SimpleImageBanner mBanner;
	private List<HomeBanner> bannerDatas;

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		receiver = new ChildReceiver();
		IntentFilter filter = new IntentFilter();
		filter.addAction(MContants.CHANGE_CURRENT_CHILD_ACTION);
		getActivity().registerReceiver(receiver, filter);
	}

	@SuppressLint("InflateParams")
	@Override
	@Nullable
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		View homeView = inflater.inflate(R.layout.fragment_home, null);
		mBanner = (SimpleImageBanner) homeView
				.findViewById(R.id.home_simple_banner);
		RelativeLayout homework = (RelativeLayout) homeView
				.findViewById(R.id.button_homework);
		homework.setOnClickListener(this);
		RelativeLayout system = (RelativeLayout) homeView
				.findViewById(R.id.button_system);
		system.setOnClickListener(this);
		RelativeLayout credit = (RelativeLayout) homeView
				.findViewById(R.id.button_credit);
		credit.setOnClickListener(this);
		RelativeLayout video = (RelativeLayout) homeView
				.findViewById(R.id.button_video);
		video.setOnClickListener(this);
		RelativeLayout grow = (RelativeLayout) homeView
				.findViewById(R.id.button_grow);
		grow.setOnClickListener(this);
		RelativeLayout money = (RelativeLayout) homeView
				.findViewById(R.id.button_money);
		money.setOnClickListener(this);
		RelativeLayout award = (RelativeLayout) homeView
				.findViewById(R.id.button_award);
		award.setOnClickListener(this);
		RelativeLayout activity = (RelativeLayout) homeView
				.findViewById(R.id.button_activity);
		activity.setOnClickListener(this);

		RelativeLayout mainList = (RelativeLayout) homeView.findViewById(R.id.button_main_list);
		mainList.setOnClickListener(this);
		homeTitle = (TextView) homeView.findViewById(R.id.home_title);

		if (mSpUtil.getUserType().equals(MContants.LOGIN_AS_PARENTS)) {
			if (currentChild != null) {
				homeTitle.setText(currentChild.name);
				homeTitle.setOnClickListener(this);
			}
		}
		LayoutParams params = mBanner.getLayoutParams();
		params.height = (int) (ScreenUtils.getScreenW(mActivity) * 356 * 1.0f / 750);
		mBanner.setLayoutParams(params);
		loadingBannerDatas();
		return homeView;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.button_homework:
			startActivity(MyHomeWorkActivity.class);
			break;
		case R.id.button_system:
			startActivity(SystemNoticeActivity.class);
			break;
		case R.id.button_credit:
			startActivity(ClassRoomCommentStatisticsActivity.class);
			break;
		case R.id.button_video:
			startActivity(ClassroomVedioActivity.class);
			break;
		case R.id.button_grow:
			startActivity(GrowthReportActivity.class);
			break;
		case R.id.button_money:
			break;
		case R.id.button_award:
			startActivity(RewardActivity.class);
			break;
		case R.id.button_activity:
			startActivity(MoveActivity.class);
			break;
		case R.id.home_title:
			showChildrenWindow(v);
			break;
		case R.id.button_main_list:
			startActivity(MainListActivity.class);
			break;	
		default:
			break;
		}
	}

	class ChildReceiver extends BroadcastReceiver {
		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction()
					.equals(MContants.CHANGE_CURRENT_CHILD_ACTION)) {
				ChildBean child = (ChildBean) intent
						.getSerializableExtra(MContants.CHANGE_CURRENT_CHILD_DATAS);
				if (child != null) {
					homeTitle.setText(child.name);
				}
			}
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		getActivity().unregisterReceiver(receiver);
	}

	@Override
	protected void onChoiceChildResult(ChildBean myChild) {
		homeTitle.setText(myChild.name);
	}

	private void loadingBannerDatas() {
		if (NetWorkUtils.isNetworkAvailable(mActivity)) {
			HashMap<String, Object> sendMap = new HashMap<String, Object>();
			sendMap.put("type", "0");
			String requestUrl = ParamsUtils.buildParams(
					NetWorkApi.HOME_BANNER_API, sendMap);
			MyAsyncHttp.get(mActivity, requestUrl, new AsyncCallBackListener() {

				@Override
				public void onAsyncCallBack(RESULTCODE resuleCode,
						String request, String result) {
					if (resuleCode == RESULTCODE.SUCCESS) {
						bannerDatas = ResponseUtils.getHomeBanners(mActivity,
								result);
						if (bannerDatas != null && bannerDatas.size() > 0) {
							showTopBannersView();
						}
					}
				}
			});
		} else {
			ShowToast.showShort(mActivity, R.string.net_break);// 提示无网络
		}
	}

	protected void showTopBannersView() {
		List<BannerItem> adListInfo = new ArrayList<BannerItem>();
		for (HomeBanner item : bannerDatas) {
			BannerItem info = null;
			info = new BannerItem();
			info.imgUrl = item.fileurl;
			info.title = item.name;
			adListInfo.add(info);
		}
		mBanner.setSelectAnimClass(RotateEnter.class)
				.setUnselectAnimClass(NoAnimExist.class).setDelay(3)
				.setPeriod(3).setSource(adListInfo).startScroll();
		mBanner.setOnItemClickL(new SimpleImageBanner.OnItemClickL() {

			@Override
			public void onItemClick(int position) {

			}
		});
	}
}
